package com.em.baseapplication;

import android.app.Application;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.maxleap.MaxLeap;

import static android.R.attr.max;

public class BaseApplication extends Application{

	private static BaseApplication instance;
	private WindowManager globalWindowManager;
	private LayoutParams globalLayout;

    private String MAX_LEAP_APPID  ="57ab1a1546e0fb0007476382";
    private String MAX_LEAP_REST_APIKEY = "S3hNNGFzWHU5UnNRTEtRcnVsN3JpUQ";
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		createWindowManager();
        //初始化MAXLEAP
        MaxLeap.initialize(this,MAX_LEAP_APPID, MAX_LEAP_REST_APIKEY,MaxLeap.REGION_CN);
	}
	
	  /**
     * 创建悬浮窗体
     */
    private void initFloatView(View v) {
    	// 声明屏幕的宽高
        v.setOnTouchListener(new OnTouchListener() {
        	float x, y;
            int top;
            long startTime;
            
            float mTouchStartX;
            float mTouchStartY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 获取相对屏幕的坐标，即以屏幕左上角为原点
                x = event.getRawX();
                y = event.getRawY() - top; // 25是系统状态栏的高度
                Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY);
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 获取相对View的坐标，即以此View左上角为原点
                    mTouchStartX = event.getX();
                    mTouchStartY = event.getY();
                    Log.i("startP", "startX" + mTouchStartX + "====startY"
                            + mTouchStartY);
                    long end = System.currentTimeMillis() - startTime;
                    // 双击的间隔在 300ms以下
                    if (end < 300) {
//                        closeDesk();
                    }
                    startTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 更新浮动窗口位置参数
                    globalLayout.x = (int) (x - mTouchStartX);
                    globalLayout.y = (int) (y - mTouchStartY);
                    globalWindowManager.updateViewLayout(v, globalLayout);
                    break;
                case MotionEvent.ACTION_UP:
                    // 更新浮动窗口位置参数
                    globalLayout.x = (int) (x - mTouchStartX);
                    globalLayout.y = (int) (y - mTouchStartY);
                    globalWindowManager.updateViewLayout(v, globalLayout);
                    // 可以在此记录最后一次的位置
                    mTouchStartX = mTouchStartY = 0;
                    break;
                }
                return true;
            }
        });
    }
	
	/**
     * 设置WindowManager
     */
    private void createWindowManager() {
        // 取得系统窗体
        globalWindowManager = (WindowManager) getApplicationContext().getSystemService("window");
        // 窗体的布局样式
        globalLayout = new WindowManager.LayoutParams();
        // 设置窗体显示类型——TYPE_SYSTEM_ALERT(系统提示)
        globalLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        // 设置窗体焦点及触摸：
        // FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)
        globalLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 设置显示的模式
        globalLayout.format = PixelFormat.RGBA_8888;
        // 设置对齐的方法
        globalLayout.gravity = Gravity.TOP | Gravity.LEFT;
        // 设置窗体宽度和高度
        globalLayout.width = WindowManager.LayoutParams.WRAP_CONTENT;
        globalLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;
    }
    
    /**
     * 显示DesktopLayout
     */
    public void showDesk(View v) {
    	initFloatView(v);
    	globalWindowManager.addView(v, globalLayout);
    }
    
    /**
     * 关闭DesktopLayout
     */
    public void closeDesk(View v) {
    	globalWindowManager.removeView(v);
    }
    
	
}
